package com.example.understandvideo.tool;

import com.example.understandvideo.signer.Signer;
import okhttp3.*;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class HttpTool {
    public static String post(String json) {
        String result = null;
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(600, TimeUnit.SECONDS) // 连接超时时间设为30秒
                .readTimeout(600, TimeUnit.SECONDS)    // 读取超时时间设为60秒
                .writeTimeout(600, TimeUnit.SECONDS)   // 写入超时时间设为30秒
                .build();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(json, mediaType);
        Request request = new Request.Builder()
                .url("https://ark.cn-beijing.volces.com/api/v3/chat/completions")
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer b30fa44c-9e05-4ca9-a8a0-47f8fb8f81c8")
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            result = response.body().string();
            System.out.println("Response: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String diyPost(String url,String json) {
        String result = null;
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(600, TimeUnit.SECONDS) // 连接超时时间设为30秒
                .readTimeout(600, TimeUnit.SECONDS)    // 读取超时时间设为60秒
                .writeTimeout(600, TimeUnit.SECONDS)   // 写入超时时间设为30秒
                .build();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(json, mediaType);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer b30fa44c-9e05-4ca9-a8a0-47f8fb8f81c8")
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            result = response.body().string();
            System.out.println("Response: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String postFormData(String picBase64) throws Exception {
        String result = null;
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(120, TimeUnit.SECONDS)  // 连接超时时间
                .readTimeout(120, TimeUnit.SECONDS)     // 读取超时时间
                .writeTimeout(120, TimeUnit.SECONDS)    // 写入超时时间
                .build();

        MultipartBody.Builder requestBodyBuilder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);

        // 添加表单参数
        requestBodyBuilder.addFormDataPart("image_base64", picBase64);
        RequestBody requestBody = requestBodyBuilder.build();
        Signer signer = new Signer();
        String formatDate = signer.getCurrentFormatDate();
        Request request = new Request.Builder()
                .url("https://visual.volcengineapi.com?Action=OCRNormal&Version=2020-08-26")
                .post(requestBody)
                .addHeader("Authorization", signer.signAuthorization(formatDate, picBase64))
                .addHeader("X-Date", formatDate)
                .build();
        System.out.println("Authorization: " + signer.signAuthorization(formatDate, picBase64));
        try (Response response = client.newCall(request).execute()) {
            result = response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String postDeepSeek(String json) {
        String result = null;
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(120, TimeUnit.SECONDS)  // 连接超时时间
                .readTimeout(120, TimeUnit.SECONDS)     // 读取超时时间
                .writeTimeout(120, TimeUnit.SECONDS)    // 写入超时时间
                .build();

        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(json, mediaType);
        Request request = new Request.Builder()
                .url("https://ark.cn-beijing.volces.com/api/v3/chat/completions")
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer b30fa44c-9e05-4ca9-a8a0-47f8fb8f81c8")
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            result = response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static String postXmlStr(String xmlString, String serverUrl) {
        String result = null;
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("text/xml;charset=utf-8");
        RequestBody body = RequestBody.create(xmlString, mediaType);
        Request request = new Request.Builder()
                .url(serverUrl)
                .addHeader("Content-Type", "text/xml;charset=utf-8")
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            result = response.body().string();
            System.out.println("Response: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getXmlStr(String serverUrl) {
        String result = null;
        // 创建 OkHttpClient 实例
        System.out.println(serverUrl);
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("text/xml;charset=utf-8");
        Request request = new Request.Builder()
                .url(serverUrl)
                .addHeader("Content-Type", "text/xml;charset=utf-8")
                .build();
        try (Response response = client.newCall(request).execute()) {
            result = response.body().string();
            System.out.println("Response: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public static void main(String[] args) {
        String result = null;
        // 创建 OkHttpClient 实例
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("text/xml;charset=utf-8");
        Request request = new Request.Builder()
                .url("http://172.17.28.98:80/api/task/233/progress")
                .addHeader("Content-Type", "text/xml;charset=utf-8")
                .get()
                .build();
        try (Response response = client.newCall(request).execute()) {
            result = response.body().string();
            System.out.println("Response: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
